package com.aifund.app.timeTask;

import com.aifund.app.entity.vo.market.MarketBean;
import com.aifund.app.websocket.MarketPushWebSocket;
import com.alibaba.fastjson.JSON;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.swing.text.html.parser.Entity;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 数字货币行情服务
 */
@Component
public class MarketQuotationsServiceImpl {

    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    MarketPushWebSocket marketPushWebSocket;

    //定时任务
    @Scheduled(fixedRate=1000)
    public void start(){
        /**
         * 获取数字币现货行情数据
         */
        Map<Object, Object> resultMap = stringRedisTemplate.opsForHash().entries("BINANCE");
        List<MarketBean> marketList = new ArrayList<>();
        for(Map.Entry<Object,Object> data : resultMap.entrySet()){
            MarketBean marketBean = JSON.parseObject(data.getValue().toString(),MarketBean.class);
            marketList.add(marketBean);
        }
        marketPushWebSocket.sendMessage(JSON.toJSONString(marketList));
    }

}
